package com.fanben100.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;

public class ProtocalUtil {
	public static String httpRequest(URL url, String json)
			throws UnknownHostException, ConnectException, IOException {
		HttpURLConnection httpURLConnection = (HttpURLConnection) url
				.openConnection();
		// httpURLConnection.setReadTimeout(30000);
		httpURLConnection.setRequestMethod("POST");
		httpURLConnection.setDoOutput(true);
		httpURLConnection.setRequestProperty("Content-Type", "UTF-8");

		// 发送部分
		BufferedOutputStream dos = null;
		dos = new BufferedOutputStream(httpURLConnection.getOutputStream());
		if (json != null) {
			dos.write(json.getBytes("UTF-8"));
		}
		dos.flush();
		dos.close();

		// 回执部分
		BufferedInputStream in = new BufferedInputStream(
				httpURLConnection.getInputStream());
		StringBuffer res = new StringBuffer();
		byte[] by = new byte[1024];
		while (in.read(by) != -1) {
			res.append(new String(by, "UTF-8"));
		}
		in.close();
		httpURLConnection.disconnect();
		return res.toString();
	}
}
